﻿using System;
using System.Windows.Forms;
using Logic;
using System.Threading;

namespace SampleDB
{
    /// <summary>
    /// SampleDBクラス
    /// </summary>
    public partial class book_management_DB : Form
    {
        public book_management_DB()
        {
            InitializeComponent();
        }

        #region イベント
        /// <summary>
        /// ロード時イベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SampleDB_Load(object sender, EventArgs e)
        {
            // GridDataの設定
            SetGridData();
        }

        /// <summary>
        /// 登録ボタン押下時処理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRegist_Click(object sender, EventArgs e)
        {
            var logic = new SampleDBLogic();
            // データ登録をする
            logic.Regist(txtName.Text, txtWriter.Text, txtIssue.Text, txtPrice.Text);

            // GridDataの再設定
            SetGridData();

            // テキストをクリアする
            txtName.Text = String.Empty;
            txtIssue.Text = String.Empty;
            txtPrice.Text = String.Empty;
            txtWriter.Text = String.Empty;
        }

        //削除ボタン押下時処理
        private void btnDelete_Click(object sender, EventArgs e)
        {
            int count = 0;
            int i =0;
            //行数取得
            foreach (DataGridViewRow row in gridData.SelectedRows)
            {
                count++;
            }
            int[] delid = new int[count];
            //選択されている行の格納
            foreach (DataGridViewRow row in gridData.SelectedRows)
            {
                delid[i] = row.Index;
                i++;
            }
            var logic = new SampleDBLogic();
            logic.Delete(delid);
        }        

        #endregion

        private void LoadGridData()
        {
            //GridDataの設定
            Thread thread = new Thread(new ThreadStart(SetGridData));
            thread.Start();
        }

        #region private
        /// <summary>
        /// Gridにデータを設定する
        /// </summary>
        private void SetGridData()
        {
            var logic = new SampleDBLogic();
            // データを取得する
            var dataList = logic.GetData();

            BindingSource bs = new BindingSource();
            if (dataList != null && dataList.Count > 0)
            {
                foreach (var item in dataList)
                {
                    bs.Add(item);
                }
            }
            BindingGridData(bs);
        }

        delegate void BindingGridDataDelegate(BindingSource bs);
        //GridDataをバインドする
        private void BindingGridData(BindingSource bs)
        {
            // UIスレッド以外から呼ばれた場合
            if (InvokeRequired)
            {
                Invoke(new BindingGridDataDelegate(BindingGridData), bs);
                return;
            }
            gridData.DataSource = bs;
        }

        #endregion

    }
}
